package com.ars.listStatistics;

import com.ars.db.DBTools;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

import javax.swing.*;
import java.net.URL;
import java.sql.ResultSet;
import java.util.ResourceBundle;

public class ShowAllListController implements Initializable {
    DBTools db = null;
    @FXML
    private TableView tableView;
    @FXML
    private TableColumn passengerNameColumn;
    @FXML
    private TableColumn passengerIdColumn;
    @FXML
    private TableColumn seatsTypeColumn;
    @FXML
    private TableColumn ticketTypeColumn;
    @FXML
    private TableColumn dateColumn;
    @FXML
    private TableColumn widColumn;
    @FXML
    private TableColumn fidColumn;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        passengerNameColumn.setCellValueFactory(new PropertyValueFactory<>("PassengerName"));
        passengerIdColumn.setCellValueFactory(new PropertyValueFactory<>("PassengerId"));
        seatsTypeColumn.setCellValueFactory(new PropertyValueFactory<>("SeatsType"));
        ticketTypeColumn.setCellValueFactory(new PropertyValueFactory<>("SeatsType"));
        dateColumn.setCellValueFactory(new PropertyValueFactory<>("Date"));
        widColumn.setCellValueFactory(new PropertyValueFactory<>("Wid"));
        fidColumn.setCellValueFactory(new PropertyValueFactory<>("Fid"));

        tableView.setItems(getData());
    }

    private ObservableList getData() {
        ObservableList data = FXCollections.observableArrayList();
        String sql = "select * from orders";
        db = new DBTools();
        ResultSet rs = db.query(sql);
        try {
            while (rs.next()) {
                List list = new List();
                list.setPassengerName(rs.getString("passenger_name"));
                list.setPassengerId(rs.getString("passenger_id"));
                list.setSeatsType(rs.getInt("seats_type"));
                list.setTicketType(rs.getInt("seats_type"));
                list.setDate(rs.getString("date"));
                list.setWid(rs.getInt("wid"));
                list.setFid(rs.getInt("fid"));

                data.add(list);

            }
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "获取信息失败！", "错误", JOptionPane.ERROR_MESSAGE);
        } finally {
            db.close();
        }
        return data;
    }


}
